package com.wzf.greattruth.rpc.frame;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RpcFramework {
    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    private RpcContainer rpcContainer;

    private int port;

    public RpcFramework(int port) {
        this.port = port;
    }

    public void start() throws IOException {
        ServerSocket server = new ServerSocket();
        server.bind(new InetSocketAddress(port));
        System.out.println("****** rpc framework started! ******");

        try {
            while (true) {
                // 1.监听客户端的TCP连接，接到TCP连接后将其封装成task，由线程池执行
                Provider provider = new Provider(server.accept(), rpcContainer);
                executor.execute(provider);
            }
        } finally {
            server.close();
            executor.shutdown();
        }
    }

    public int getPort() {
        return port;
    }

    public RpcContainer getRpcContainer() {
        return rpcContainer;
    }

    public void setRpcContainer(RpcContainer rpcContainer) {
        this.rpcContainer = rpcContainer;
    }

}
